/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package inc.veluvana.weatherforecaster

/**
 *
 * @author victor
 */
//import dinamyc of libs

//para crear el archivo
import java.io.FileOutputStream
import java.io.IOException
//para crear el pdf
import com.itextpdf.text.Document
import com.itextpdf.text.DocumentException
import com.itextpdf.text.Paragraph
import com.itextpdf.text.pdf.PdfWriter
import com.itextpdf.text.pdf.PdfPTable
import com.itextpdf.text.*
import com.itextpdf.text.pdf.PdfPCell


class PdfGenerator{
    String msg
    //number of Sales check
    //    static int num
    def principal(Order o) throws DocumentException, IOException {
        //name of pdf
        //String documento="Bill.pdf"
        Document document=new Document()
        //write the document
        PdfWriter.getInstance(document,new FileOutputStream("Bill.pdf"))
        //open of document
        document.open()
        //add content to pdf
        
        document.add(new Paragraph("Sales Check", FontFactory.getFont("Arial",25,Font.BOLD,BaseColor.BLUE)));
        createBlankLines(document,2)
        if(o){
            document.add(new Paragraph("Date: "+new Date().toString()))
            document.add(new Paragraph("Buyer Name: "+o.getBuyerName()))
            document.add(new Paragraph("Adress of Delivery: "+o.getDeliveryAddress()))
            createBlankLines(document,2)
            PdfPTable table=new PdfPTable(5)
            table.setWidthPercentage(100)
            //cells title
            PdfPCell cell1, cell2, cell3, cell4, cell5
            cell1 = new PdfPCell(new Paragraph("Wood", FontFactory.getFont("Arial", 10, Font.BOLD, BaseColor.BLACK)));
            cell2 = new PdfPCell(new Paragraph("Amount", FontFactory.getFont("Arial", 10, Font.BOLD, BaseColor.BLACK)));
            cell3 = new PdfPCell(new Paragraph("Unit value", FontFactory.getFont("Arial", 10, Font.BOLD, BaseColor.BLACK)));
            cell4 = new PdfPCell(new Paragraph("Delivery time", FontFactory.getFont("Arial", 10, Font.BOLD, BaseColor.BLACK)));
            cell5 = new PdfPCell(new Paragraph("SubTotal", FontFactory.getFont("Arial", 10, Font.BOLD, BaseColor.BLACK)))
            //add cells
            table.addCell(cell1)
            table.addCell(cell2)
            table.addCell(cell3)
            table.addCell(cell4)
            table.addCell(cell5)
                
            o.getItems().each{ k, v -> 
                cell1 = new PdfPCell(new Paragraph(k.cannonicalName))
                cell2 = new PdfPCell(new Paragraph(v.toString()))
                cell3 = new PdfPCell(new Paragraph(k.unitPrice.toString()))
                cell4 = new PdfPCell(new Paragraph(k.deliveryTime.toString()))
                cell5 = new PdfPCell(new Paragraph((k.unitPrice*v).toString()))
                table.addCell(cell1)
                table.addCell(cell2)
                table.addCell(cell3)
                table.addCell(cell4)
                table.addCell(cell5)
            }

            cell1=new PdfPCell(new Paragraph("Purchase Price: ", FontFactory.getFont("Arial", 10, Font.BOLD, BaseColor.BLACK)))
            cell1.setColspan(4)
            table.addCell(cell1)
            cell5=new PdfPCell(new Paragraph(o.getPurchasePrice()+""))
            table.addCell(cell5)
                
            document.add(table)
            msg="Pdf was created successfully"
                
        }
        else{
            msg="Pdf was fail" 
        }
        
        document.close()
        
        return msg
    }
    
    def createBlankLines(doc,lines){
        for(int i=0;i<lines;i++){
            doc.add(new Paragraph(" "))
        }
    }
}


